package com.brkj.communityStudy;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import com.brkj.communityStudy.model.DS_QAComment;
import com.brkj.communityStudy.model.DS_QAStudy;
import com.brkj.communityStudy.model.DS_TopicContent;
import com.brkj.course.CommentAdapter;
import com.brkj.course.CourseDetailActivity;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.httpInterface.NomalResult;
import com.brkj.main.R;
import com.brkj.model.DS_Course;
import com.brkj.util.ImgShow;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseActivity;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.WidgetAnim;
import com.dgl.sdk.view.NoSlipGridView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import org.json.JSONArray;
import org.json.JSONException;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/19.
 */
public class QADetailActivity extends BaseActivity{

    @ViewInject(id= R.id.commentListView)
    ListView commentListView;
    @ViewInject(id=R.id.commentEdit)
    EditText commentEdit;
    @ViewInject(id=R.id.submit,click="submitComment")
    Button submit;

    private View QAHeadView;

    private QACommentAdapter commentAdapter;
    protected List<DS_QAComment> commentList = new ArrayList<DS_QAComment>();
    private View lodingView;
    protected DS_QAStudy qa;

    private TextView title;
    private TextView content;
    private TextView qTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setAnimType(RIGHT_ENTER_LEFT_EXT);

        super.onCreate(savedInstanceState);

        setContentView(R.layout.course_detail);

        setActivityTitle("问答详情");
        setReturnBtn();

        getQAData();

        initLodingBar();

        commentEdit.clearFocus();

        commentListView.addHeaderView(QAHeadView);
        commentListView.addHeaderView(lodingView);

        commentAdapter = new QACommentAdapter(QADetailActivity.this, commentList,qa.getQuestionID(),  false);
        commentListView.setAdapter(commentAdapter);

        getComment();
    }

    protected void getQAData() {
        qa = (DS_QAStudy)getIntent().getSerializableExtra("QA");

        initQA();
    }

    protected void initQA() {
        QAHeadView = getLayoutInflater().inflate(
                R.layout.qa_detail_content, null, false);

        content = (TextView) QAHeadView.findViewById(R.id.content);
        qTime = (TextView) QAHeadView.findViewById(R.id.time);

        content.setText(qa.getQuestion());
        qTime.setText(qa.getTime());
    }

    private void initLodingBar() {
        lodingView = getLayoutInflater().inflate(
                R.layout.loding_bar, null, false);

    }


    protected void getComment() {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetQAAnswer.params.QuestionID, Integer.toString(qa.getQuestionID()));

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetQAAnswer.address, params, new AjaxCallBack<Object>() {


            @Override
            public void onFailure(Throwable t, String strMsg) {
                // TODO Auto-generated method stub
                super.onFailure(t, strMsg);

                lodingView.setVisibility(View.GONE);
            }

            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

//				HIF_GetCourse.result result = JsonUtil.fromJson((String)t, HIF_GetCourse.result.class);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_QAComment>>(){}.getType();
                commentList  = gson.fromJson((String)t, listType);

                if(commentList != null){
                    getView(commentList);
                }

                lodingView.setVisibility(View.GONE);
            }

        });

    }

    public void getView(List<DS_QAComment> list){
        boolean isShowSlelectBestAnswer = false;

        if(Integer.toString(qa.getAskerID()).equals(MyApplication.myUserID)){
            isShowSlelectBestAnswer = true;

            for(DS_QAComment qaComment:list){
                if(qaComment.getIsBest() == 1){
                    isShowSlelectBestAnswer = false;
                    break;
                }
            }
        }

        commentAdapter = new QACommentAdapter(QADetailActivity.this, list,qa.getQuestionID(), isShowSlelectBestAnswer);
        commentListView.setAdapter(commentAdapter);
    }

    public void submitComment(View v){
        if(WidgetAnim.checkIfEmpty(this, commentEdit)){
            return;
        }

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_SubmitQAComment.params.QuestionID, qa.getQuestionID()+"");
        params.put(HttpInterface.HIF_SubmitQAComment.params.UserID, MyApplication.myUserID);
        params.put(HttpInterface.HIF_SubmitQAComment.params.CommentText, commentEdit.getText().toString());

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_SubmitQAComment.address, params, new MyAjaxCallBack<Object>(this) {

            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                if(NomalResult.doResult((String)t, QADetailActivity.this)){
                    showToast("回答成功！");
                    commentEdit.setText("");
                    getComment();

                    //隐藏键盘
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(commentEdit.getWindowToken(), 0); //强制隐藏键盘
                };

            }

        });
    }

}
